import {
    getHomeCasual,
    getHomeNav,
    getHomeShopList,
    getRecommendShopList,
    getSearchGoods,
    getUserInfo,
    getLogOut
} from '../api'

import {
    HOME_CASUAL,
    HOME_NAV,
    HOME_SHOP_LIST,
    RECOMMEND_SHOP_LIST,
    SEARCH_GOODS,
    USER_INFO,
    RESET_USER_INFO
} from './mutation-types'

export default {
    // 1.获取首页轮播图
    async reqHomeCasual({commit}) {
        const result = await getHomeCasual();
        commit(HOME_CASUAL, {homecasual: result.message})
        
    },
    // 2.获取首页导航
    async reqHomeNav({commit}) {
        const result = await getHomeNav();
        commit(HOME_NAV, {homenav: result.message.data})
        
    },
    // 3.获取首页导航
    async reqHomeShopList({commit}) {
        const result = await getHomeShopList();
        commit(HOME_SHOP_LIST, {homeshoplist: result.message.goods_list})
        
    },

    // 4.请求推荐数据
    async reqRecommendShopList({commit},params) {
        console.log(params);
        const result = await getRecommendShopList(params);
        commit(RECOMMEND_SHOP_LIST, {recommendShopList: result.message})
        params.callback && params.callback()
    },
    // 5.请求搜索数据
    async reqSearchGoods({commit}) {
        const result = await getSearchGoods();
        commit(SEARCH_GOODS, {searchgoods: result.message.data})
        
    },

    // 6. 同步获取用户数据
    asyncUserInfo({commit}, userInfo) {
        commit(USER_INFO, {userInfo})
    },

    // 7. 异步获取用户信息
    async getUserInfo({commit}) {
        const result = await getUserInfo();
        console.log(result);
        if (result.success_code === 200) {
            commit(USER_INFO, {userInfo: result.message})
        }
    },

    // 8. 退出登录
    async logout({commit}) {
        const result = await getLogOut();
        console.log(result);
        if (result.success_code === 200) {
            commit(RESET_USER_INFO)
        }
    }
}